﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.Management.Controllers
{
    public class BannerTransactionListController : Controller
    {
        // Create an instance of user manage
        LeadExpress.Repository.BannerTransactionManagementRepository repository = new LeadExpress.Repository.BannerTransactionManagementRepository();

        // GET: /Management/Home/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            BannerTransactionManagementModel model = new BannerTransactionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 10, 1);

            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Fillter(string keywords, string startDate, string endDate, string optionId, string paymentId, int pageSize, int pageIndex, int id, string sortBy, string sortOrder)
        {
            BannerTransactionManagementModel model = new BannerTransactionManagementModel();
            
            // Parse data
            string[] options = optionId.Split(',');
            string[] payment = paymentId.Split(',');

            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", keywords, startDate, endDate, options, payment, sortBy, sortOrder, pageSize, pageIndex, id);

            return Json(model);
        }

        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            BannerTransactionManagementModel model = new BannerTransactionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);

            ViewBag.TransactionId = id;

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            BannerTransaction model = repository.GetSingle(id);

            if (model != null)
            {
                model.BannerName = form["BannerName"].ToString();

                // Option
                if (form["OptionId"] != null && int.Parse(form["OptionId"].ToString()) != 0)
                {
                    int optionId = int.Parse(form["OptionId"].ToString());
                    model.OptionID = optionId;
                }

                // Payment
                if (form["PaymentId"] != null && int.Parse(form["PaymentId"].ToString()) != 0)
                {
                    int paymentId = int.Parse(form["PaymentId"].ToString());
                    model.PaymentID = paymentId;
                }

                // Start Date
                DateTime isStartDate;
                if (form["StartDate"] != null && DateTime.TryParse(form["StartDate"].ToString(), out isStartDate))
                {
                    model.StartDate = Convert.ToDateTime(form["StartDate"].ToString());
                }

                // End Date
                DateTime isEndDate;
                if (form["EndDate"] != null && DateTime.TryParse(form["EndDate"].ToString(), out isEndDate))
                {
                    model.EndDate = Convert.ToDateTime(form["EndDate"].ToString());
                }

                // List Price
                decimal isListPrice;
                if (form["ListPrice"] != null && decimal.TryParse(form["ListPrice"].ToString(), out isListPrice))
                {
                    model.ListPrice = decimal.Parse(form["ListPrice"].ToString());
                }
                model.UnitPrice = form["UnitPrice"].ToString();

                // Transaction
                model.TransactionGuid = Guid.NewGuid().ToString();
                model.TransactionDate = DateTime.Now;

                repository.Update(model);
            }

            return RedirectToAction("Index", "BannerTransactionList");
        }
        
        #region Report Banners
        // GET: /Management/Home/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Report()
        {
            BannerTransactionManagementModel model = new BannerTransactionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 10, 1);

            return View(model);
        }

        // Export a list banner
        public FileResult ExportToExcel()
        {
            FileResult fileReport = repository.ExportToCSVFile();

            return fileReport;
        }
        #endregion
    }
}
